package com.ncmmall.api.apis.service;

import com.feijin.commons.lang.PropertiesHelps;
import com.feijin.commons.lang.Requires;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author linxs
 *         短信验证码校验
 */
public class MsmCheck {

    private static Logger log = LoggerFactory.getLogger(MsmCheck.class);

    public static void checkMobileCode(String mobile, String mobileCode) throws IllegalAccessException {
        Requires.hasLength(mobile, "手机号码不能为空");
        Requires.hasLength(mobileCode, "手机验证码不能为空");

        String value = "";//(String) Securitys.getSession().getAttribute(mobile);
        log.info("mobile=" + mobile + ",mobileCode=" + mobileCode + ",session value=" + value);
        if (StringUtils.isEmpty(value)) {
            throw new IllegalAccessException("非法请求");
        }

        String[] arr = value.split("-");
        if (arr.length < 2) {
            throw new IllegalAccessException("非法请求");
        }
        if (!mobileCode.equals(arr[1])) {
            throw new IllegalAccessException("短信验证码错误");
        }

        Long codeTime = NumberUtils.toLong(arr[0]);
        if (codeTime <= 0) {
            throw new IllegalAccessException("非法请求");
        }

        Long interval = NumberUtils.toLong(PropertiesHelps.getValue("mobilecode.interval"));
        Long intervalTime = System.currentTimeMillis() - codeTime;

        log.info("intervalTime=" + intervalTime);
        if (intervalTime > interval * 1000) {
            throw new IllegalAccessException("验证码已过期");
        }
    }


}
